package com.csnf.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybaitsUtil {
    private static SqlSessionFactoryBuilder builder;
    private static SqlSessionFactory factory;
    private static SqlSession session;
    private static InputStream input;

    public static SqlSession init(){
        try {
            input = Resources.getResourceAsStream("mybatis-config.xml");
            builder = new SqlSessionFactoryBuilder();
            factory = builder.build(input);
            session = factory.openSession(true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return session;
    }

    public static void close(){
        session.close();
    }
}
